import { createRouter, createWebHashHistory } from 'vue-router';
const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('@/views/home/Home'),
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/Login'),
    // beforeEnter(to, from, next) {// 路由独享守卫
    //   const isLogin = localStorage.isLogin;
    //   //已经登录状态访问login跳转到主页
    //   // isLogin?next({name:'Home'}):next();
    // }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/register/Register'),
    // beforeEnter(to, from, next) {// 路由独享守卫
    //   const isLogin = localStorage.isLogin;
    //   //已经登录状态访问login跳转到主页
    //   // isLogin?next({name:'Home'}):next();
    // }
  },
  {
    path: '/shop/:id',
    name: 'Shop',
    component: () => import('@/views/shop/Shop'),
  },
  {
    path: '/cartlist',
    name: 'cartlist',
    component: () => import('@/views/cartList/CartList'),
  },
  {
    path: '/orderConfirmation/:id',
    name: 'OrderConfirmation',
    component: () => import('@/views/orderConfirmation/OrderConfirmation'),
  },
  {
    path: '/test',
    name: 'test',
    component: () => import('@/views/shop/test'),
  },
  {
    path: '/orderList',
    name: 'orderList',
    component: () => import('@/views/orderList/OrderList'),
  },
  {
    path: '/search',
    name: 'search',
    component: () => import('@/views/search/Search'),
  },
  {
    path: '/searchList',
    name: 'searchList',
    component: () => import('@/views/search/SearchList'),
  },
  {
    path: '/address',
    name: 'address',
    component: () => import('@/views/address/Address'),
  },
  {
    path: '/addressEdit',
    name: 'addressEdit',
    component: () => import('@/views/addressEdit/AddressEdit'),
  },
  {
    path: '/addressSelect',
    name: 'addressSelect',
    component: () => import('@/views/address/AddressSelect'),
  },
];
const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
// 全局前置守卫
router.beforeEach((to, from, next) => {
  const { isLogin } = localStorage;
  if (!isLogin && to.name !== 'Login' && to.name !== 'Register') {
    next({ name: 'Login' });
  } else {
    next();
  }
});
export default router;
